package org.mtmi.ui.controls.popups;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Path;
import org.eclipse.swt.graphics.Point;
import org.mtmi.ui.GraphicUtil;
import org.mtmi.ui.M;
import org.mtmi.ui.controls.Button;
import org.mtmi.ui.controls.listeners.TapAdapter;
import org.mtmi.ui.controls.listeners.TapEvent;
import org.mtmi.ui.controls.listeners.PopupListener;
import org.mtmi.ui.scenes.COLOR;
import org.mtmi.ui.scenes.FontManager;
import org.mtmi.ui.scenes.Scene;

/**
 * Popup with two buttons. Default buttons (short constructor) are "Ok" and "Cancel".<br>
 * This popup is typically used to asked something or a confirmation to the user.<br>
 * All elements existing in the display is darkened and can no longer be touched.<br>
 * The popup gain the entire focus until one of the buttons is touched.<br>
 * <pre>
		TwoButtonsPopup popup = new TwoButtonsPopup(this, "Are you happy today?", new String[]{"Yes", "No"});
		popup.addResponseListener(new TwoButtonsResponseListener(){
			public void buttonTapped(int index) {
				if(index == 0) {
					System.out.println("Yes tapped");
				} else {
					System.out.println("No tapped");
				}
			}
		});
 </pre>
 * @author jgout
 *
 */
public class TwoButtonsPopup extends Popup {

	private static final int hSpan = 50;
	private static final int hMargin = 50;
	private static final int vMargin = 50;
	private static final int vSpan = 150;
	private String question;
	private Button button1;
	private Button button2;
	private Point questionSize;
	private int touched;
	private final List<PopupListener> listeners;
	
	public TwoButtonsPopup(Scene scene, String question, int flags ) {
		this( scene, question, new String[]{"Ok", "Cancel"}, flags );
	}

	public TwoButtonsPopup(Scene scene, String question, String[] buttons, int flags ) {
		super( scene, flags );
		this.question = question; 
		this.listeners = new ArrayList<PopupListener>();
		computeQuestionSize();
		createButtons(buttons);
	}

	private void computeQuestionSize() {
		int limit = getScene().getSize().x/2;
		questionSize = getSize(question);
		if(questionSize.x > limit) {
			//- question is too large, need to cut it.
			question = cutText(question);
			//- final size
			questionSize = getSize(question);
		}
	}

	private void createButtons(String[] buttons) {
		button1 = new Button(getScene(), flags & M.SCENE_EDITOR_CONTROL );
		button1.setText(buttons[0]);
		button2 = new Button(getScene(), flags & M.SCENE_EDITOR_CONTROL );
		button2.setText(buttons[1]);
		int x = getSize().x/2 - button1.getSize().x - hSpan;
		int y = getSize().y/3 + questionSize.y+vSpan;
		button1.setLocation(x, y);
		button2.setLocation(getSize().x/2+ hSpan, y);
		button1.addTapListener(new TapAdapter() {
			@Override
			public void controlTappedDown(TapEvent event) {
				touched = 0;
				terminate();
			}
		});
		button2.addTapListener(new TapAdapter() {
			@Override
			public void controlTappedDown(TapEvent event) {
				touched = 1;
				terminate();
			}
		});
	}

	private void terminate() {
		for (PopupListener l : listeners) {
			l.popupButtonSelected(touched);
		}
		dispose();
		button1.dispose();
		button2.dispose();
	}

	public void addResponseListener(PopupListener listener) {
		if(!listeners.contains(listener)) {
			listeners.add(listener);
		}
	}

	public void removeResponseListener(PopupListener listener) {
		listeners.remove(listener);
	}

	@Override
	public void paintContent(GC gc, int w, int h) {
		gc.setForeground( COLOR.sceneFg );
		gc.setBackground( COLOR.sceneBg );
		gc.setLineWidth(4);
		int height = questionSize.y+vSpan+Math.max(button1.getSize().y, button2.getSize().y)+(2*vMargin);
		Path path = GraphicUtil.getRoundRectangle(gc, (w-questionSize.x)/2 - hMargin, h/3-vMargin, questionSize.x+2*hMargin, height, hMargin, vMargin);
		gc.fillPath(path);
		gc.setFont(FontManager.getInstance().getNormalFont());
		gc.drawText(question, (w-questionSize.x)/2, h/3);
		gc.drawPath(path);
	}

	@Override
	public String toString() {
		//- 20 first characters of the question
		return question.substring(0, 20);
	}
}
